home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / gag / Oing.lha / Oing / oing.c < prev    next >
C/C++ Source or Header  |  1986-09-01  |  4KB  |  191 lines

  1. /*  :ts=8  bk=0
  2.  * If you liked "Boing!", there's a better than even chance you'll like this.
  3.  *
  4.  * Leo L. Schwab        8608.6
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <intuition/intuition.h>
  10. #include <graphics/sprite.h>
  11.  
  12. #define REV        0L
  13. #define FRAMES        6
  14. #define SPRHEIGHT    16
  15. #define WORDSPERSPR    (2 * SPRHEIGHT + 4)
  16. #define MAXX        (640-32)
  17. #define MAXY        (200-16)
  18. #define MAGIC1        33
  19. #define MAGIC2        21
  20. #define ever        (;;)
  21.  
  22. extern UWORD    ballmask[], ball0[];
  23. extern void    *OpenLibrary(), *OpenWindow(), *AllocMem(), *GetMsg(),
  24.         *ViewPortAddress();
  25. extern long    GetSprite(), VBeamPos();
  26.  
  27.  
  28. struct NewWindow windef = {
  29.     0, 15, 300, 10,
  30.     -1, -1,
  31.     CLOSEWINDOW,
  32.     WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG | ACTIVATE,
  33.     NULL, NULL,
  34.     (UBYTE *) "One moment....",
  35.     NULL, NULL, 0, 0, 0, 0,
  36.     WBENCHSCREEN
  37. };
  38. /*    Doing it this way is faster than saying  (i+offset) % 6  */
  39. UBYTE        idx[] = { 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 };
  40.  
  41. struct SimpleSprite    spr[FRAMES];
  42. struct Window    *win;
  43. struct ViewPort    *vp;
  44. UWORD        *sprbuf;
  45. UWORD        *sprites[FRAMES];
  46. int        vx[FRAMES], vy[FRAMES];
  47. void        *GfxBase, *IntuitionBase;
  48.  
  49.  
  50. main ()
  51. {
  52.     int i, offset = 0, flag = 0;
  53.     void *msg;
  54.  
  55.     openstuff ();
  56.     setupsprites ();
  57.     rnd ((short) -VBeamPos());    /*  Plant a seed  */
  58.  
  59.     for (i=0; i<FRAMES; i++) {
  60.         if (GetSprite (&spr[i], (long) i+2) < 0)
  61.             die ("Sprite allocation failed.");
  62.         spr[i].x = 640 / 2;
  63.         spr[i].y = 10;
  64.         spr[i].height = SPRHEIGHT;
  65.         ChangeSprite (vp, &spr[i], sprites[i]);
  66.     }
  67.  
  68.     SetWindowTitles (win, "  \253\253<< Oing! >>\273\273  ", "Oing!");
  69.     for ever {
  70.         /*
  71.          * WaitTOF()'s presence way up here is significant.  I used
  72.          * have it down by the ChangeSprite() loop, but it flickered.
  73.          * When it's up here, I get just the right amount of delay
  74.          * after top-of-frame so that nothing glitches.
  75.          */
  76.         WaitTOF ();
  77.         if (msg = GetMsg (win -> UserPort)) {
  78.             ReplyMsg (msg);
  79.             closestuff ();
  80.             return;
  81.         }
  82.         for (i=0; i<FRAMES; i++) {
  83.             spr[i].x += vx[i];
  84.             if (spr[i].x > MAXX) {
  85.                 /*  spr.x is unsigned, so we have to cheat  */
  86.                 spr[i].x = (spr[i].x & 0x8000) ? 0 : MAXX;
  87.                 vx[i] = -vx[i];
  88.             }
  89.  
  90.             /* The shift by 2 is to slow vertical motion a bit */
  91.             if ((spr[i].y += (vy[i] >> 2)) > MAXY) {
  92.                 spr[i].y = MAXY;
  93.                 vx[i] = rnd (MAGIC2) - MAGIC2 / 2;
  94.                 vy[i] = -rnd (MAGIC1) - 5;
  95.             }
  96.             vy[i]++;    /*  Gravity  */
  97.         }
  98.  
  99.         for (i=0; i<FRAMES; i++) 
  100.             ChangeSprite (vp, &spr[i], sprites[idx[i+offset]]);
  101.  
  102.         /*  Rotate balls every other loop  */
  103.         if (flag = !flag)
  104.             offset = (offset + 1) % 6;
  105.     }
  106. }
  107.  
  108. openstuff ()
  109. {
  110.     register int i;
  111.  
  112.     if (!(IntuitionBase = OpenLibrary ("intuition.library", REV)))
  113.         die ("Intuition open failed.");
  114.  
  115.     if (!(GfxBase = OpenLibrary ("graphics.library", REV)))
  116.         die ("Art shop closed.");
  117.  
  118.     if (!(win = OpenWindow (&windef)))
  119.         die ("Window painted shut.");
  120.     vp = ViewPortAddress (win);
  121.  
  122.     for (i=20; i<32; i += 4) {
  123.         SetRGB4 (vp, i+1L, 15L, 0L, 0L);
  124.         SetRGB4 (vp, i+3L, 15L, 14L, 13L);
  125.     }
  126. }
  127.  
  128. closestuff ()
  129. {
  130.     register int i;
  131.  
  132.     for (i=0; i<FRAMES; i++)
  133.         if (spr[i].num)
  134.             FreeSprite ((long) spr[i].num);
  135.  
  136.     if (sprbuf)
  137.         FreeMem (sprbuf, 2L * WORDSPERSPR * FRAMES);
  138.     if (win)
  139.         CloseWindow (win);
  140.     if (GfxBase)
  141.         CloseLibrary (GfxBase);
  142.     if (IntuitionBase)
  143.         CloseLibrary (IntuitionBase);
  144. }
  145.  
  146. die (str)
  147. char *str;
  148. {
  149.     if (win) {
  150.         SetWindowTitles (win, str, -1L);
  151.         WaitPort (win -> UserPort);
  152.     } else
  153.         puts (str);
  154.     closestuff ();
  155.     exit (100);
  156. }
  157.  
  158. /*
  159.  * The following code segment was lifted (nearly) intact from the Alpha-9
  160.  * 1.2 README disk.  Thanks to Jim Mackraz for the code, and also for
  161.  * drawing all those ball sprites.
  162.  */
  163. setupsprites ()
  164. {
  165.     UWORD *cw;    /* current position in buffer of sprite images */
  166.     UWORD *maskp;    /* current position in ballmask             */
  167.     UWORD *ballp;    /* current position in ball0 data           */
  168.     int frame, scan;
  169.  
  170.     if (!(sprbuf = AllocMem (2L * WORDSPERSPR * FRAMES, MEMF_CHIP)))
  171.         die ("Can't allocate sprite buffer.");
  172.  
  173.     cw = sprbuf;    /* current position at top of buffer */
  174.     ballp = ball0;    /* ... top of data to be interleaved */
  175.  
  176.     for (frame=0; frame<FRAMES; frame++) {
  177.         maskp = ballmask;    /* one mask for all frames */
  178.         sprites[frame] = cw;
  179.         *cw++ = 0;        /* poscntl */
  180.         *cw++ = 0;
  181.  
  182.         /* one word from ball0, one word from ballmask */
  183.         for (scan=0; scan<SPRHEIGHT; scan++) {
  184.             *cw++ = *maskp++;
  185.             *cw++ = *ballp++;
  186.         }
  187.         *cw++ = 0;    /* termination */
  188.         *cw++ = 0;
  189.     }
  190. }
  191.